package com.simpou.tests.rest.helper;

import com.simpou.tests.rest.helper.ClientResponseHelper;
import com.simpou.tests.rest.ResponseWriterTester;
import com.simpou.tests.rest.RestClientServerTester;
import com.simpou.tests.rest.cache.CacheableClient;
import com.simpou.tests.rest.service.RestService;
import com.simpou.commons.utils.tests.TestRunner;
import com.simpou.commons.utils.tests.annot.ClassTest;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import org.junit.runner.RunWith;

@ClassTest(ClientResponseHelper.class)
@RunWith(TestRunner.class)
public class ClientResponseHelperITCase {

    private static RestClientServerTester rest;

    @BeforeClass
    public static void setUpClass() throws IOException {
        rest = new RestClientServerTester(2223, new CacheableClient(),
                RestService.class.getPackage().getName());
    }

    @Test
    public void testFromCache() {
        WebResource resource = rest.getResource("cached/object");
        ClientResponse response;

        assertFalse(ClientResponseHelper.fromCache(null));

        // não está em cache
        response = resource.get(ClientResponse.class);
        assertFalse(ClientResponseHelper.fromCache(response));

        // entrou em cache
        response = resource.get(ClientResponse.class);
        assertTrue(ClientResponseHelper.fromCache(response));
    }
}
